<template>
  <el-dialog :title="dialogProps.title" :visible.sync="dialogProps.visible" width="560px"
    @open="handleDialogOpen()" v-loading="loading">
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>
    <el-form :model="vaccinModel" :rules="formRules"
      ref="foodBankForm" label-width="80px" label-position="right" size="small">
      <el-form-item label="厂家名称" prop="deptName">
        <el-col :span="16"><el-input ref="deptName" v-model="vaccinModel.deptName" autofocus></el-input></el-col>
      </el-form-item>
      <el-form-item label="疫苗类型" prop="vaccinType">
        <el-col :span="16"><el-input  v-model="vaccinModel.vaccinType"></el-input></el-col>
      </el-form-item>
      <el-form-item label="厂家地址" prop="deptAddress">
        <el-col :span="13"><el-input  v-model="vaccinModel.deptAddress"></el-input></el-col>
      </el-form-item>
      <el-form-item label="厂家描述" prop="deptDesc">
        <el-col :span="13"><el-input  v-model="vaccinModel.deptDesc"></el-input></el-col>
      </el-form-item>
      <el-form-item label="总针数" prop="setCount">
        <el-col :span="13">
          <el-input-number v-model="vaccinModel.setCount" :min="1"  ></el-input-number>
          </el-col>
      </el-form-item>
      <el-form-item label="当天可预约总针数" prop="">
        <el-col :span="13"><el-input  v-model="vaccinModel.tVaccinNoteDept.daySumCount"></el-input></el-col>
      </el-form-item>
      <el-form-item label="已预约预约针数" prop="" v-show="dialogProps.action=='edit'">
        <el-col :span="13"><el-input  v-model="vaccinModel.tVaccinNoteDept.orderCount"></el-input></el-col>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
      <el-button type="primary" size="small" @click="submitForm('foodBankForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {  updateTVaccinDept,saveTVaccinDept } from '@/api/xsf/vaccinSet'

export default {
  name: 'role-edit',
  data() {
    return {
      loading: false,
      vaccinModel: {
        id: 0,
        deptName: '',
        deptAddress:'',
        setCount:0,
        deptDesc:'',
        vaccinType:'',
        tVaccinNoteDept:{
          daySumCount:0,
          orderCount:0
        }
      },
      dialogProps: {
        visible: false,
        action: '',
        title: ''
      },
      formRules: {
        deptDesc: [
          { required: true, message: '请输入厂家描述', trigger: 'blur' },
        ],
        deptName: [
          { required: true, message: '请输入厂家名称', trigger: 'blur' },
        ],
        deptAddress: [
          { required: true, message: '请输入厂家地址', trigger: 'blur' },
        ],
        vaccinType: [
          { required: true, message: '请输入疫苗类型', trigger: 'blur' },
        ],
        daySumCount: [
          { required: true, message: '请输入当日可预约总针数', trigger: 'blur' },
        ],
        orderCount: [
          { required: true, message: '请输入已预约预约总针数', trigger: 'blur' },
        ]
      },
      options:[]

    }
  },
  methods: {

    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if(this.dialogProps.action === 'add') {
            this.doSaveRole()
          } else {
            this.doUpdateRole()
          }
        } else {
          return false
        }
      });
    },
    doUpdateRole() {
      this.loading = true
      updateTVaccinDept(this.vaccinModel.id, this.vaccinModel)
      .then(response => {
        this.loading = false
        this.dialogProps.visible = false
        this.$emit('save-finished')
      })
      .catch(error => {
        this.outputError(error)
      })
    },
    doSaveRole() {
      this.loading = true
      saveTVaccinDept(this.vaccinModel)
      .then(response => {
        this.loading = false
        this.dialogProps.visible = false
        this.$emit('save-finished')
      })
      .catch(error => {
        this.outputError(error)
      })
    },
    handleDialogClose() {
      this.dialogProps.visible = false
    },
    handleDialogOpen() {
      this.$nextTick(() => {
        this.$refs['foodBankForm'].clearValidate()
        this.$refs['deptName'].focus()
      })
    },
    outputError(error) {
      console.log(error.response ? error.response : error.message)
      this.loading = false
      this.$message({
        showClose: true,
        message: '出错了，请按F12查看浏览器日志。',
        type: 'error'
      })
    },
  /*  initSelectOption(){
      listTMealsType().then((response)=>{
        this.options=response.data;
      })
    },*/
  },
  mounted: function() {
    //this.initSelectOption();

    this.$nextTick(() => {
      this.$on('openEditRoleDialog', function(role) {
        this.vaccinModel = role
        //this.$set(this.vaccinModel,'typeid',Number.parseInt(role.typeid))

        this.dialogProps.action = 'edit'
        this.dialogProps.title = '修改疫苗信息'
        this.dialogProps.visible = true
      })
      this.$on('openAddRoleDialog', function() {
        alert('msg');
        this.dialogProps.visible = true
        this.dialogProps.action = 'add'
        this.dialogProps.title = '添加疫苗信息'
        this.vaccinModel = {
          daySumCount: 0
        }
      })
    })
  }
}
</script>

